import { defineComponent } from "vue";
import TreeFilter from "@/components/TreeFilter/index.vue";
import { getGatewayList } from "@/api/modules/iot/gateway";
import { GatewayType } from "@/api/interface/iot";
export default defineComponent({
	props: {
		// todo: 需要改造成v-model
		detail: {
			type: Object,
			default() {
				return {};
			}
		}
	},
	emits: [],
	setup(props) {
		const gatewayTreeCallback = (data: any) => {
			return data.records;
		};
		const changeGateway = (data: any) => {
			props.detail.gatewaySn = data.sn;
		};
		const status = reactive<any>({
			dataList: []
		});
		onMounted(async () => {
			getDataList();
		});
		const getDataList = async () => {
			let dataList = [];
			try {
				let res = await getGatewayList({ pageIndex: 1, pageSize: 9999 }, GatewayType.SoftGateway);
				dataList = res.result?.records || [];
				res = await getGatewayList({ pageIndex: 1, pageSize: 9999 }, GatewayType.Gateway);
				dataList = [...dataList, ...(res.result?.records || [])];
			} catch (e) {
				console.log(e);
			}
			status.dataList = dataList;
		};
		return () => {
			return (
				<>
					<div class="w-full">
						{true ? null : (
							<TreeFilter
								requestApi={getDataList}
								label="deptName"
								dataCallback={gatewayTreeCallback}
								defaultValue={props.detail.gatewaySn}
								isCheckStrictly={true}
								clearable
								onChange={(data: any) => changeGateway(data)}
							/>
						)}
						<el-select class="w-full" v-model={props.detail.gatewaySn} placeholder="请选择接入网关" clearable>
							{status.dataList.map((item: any) => {
								return <el-option label={item.name || item.sn} value={item.sn} />;
							})}
						</el-select>
					</div>
				</>
			);
		};
	}
});
